<?php

require_once( 'flexical/model/object.php' );
require_once( 'flexical/model/feed.php' );

require_once( 'flexical/admin/view.php' );
require_once( 'flexical/admin/cud-response.php' );
require_once( 'flexical/admin/feed-response.php' );
require_once( 'flexical/admin/message.php' );

class Flexical_Admin_Edit_Feed_Form_Renderer
{
	private $view;
	private $feed;

	public function __construct( $view = null, $feed = null )
	{
		$this->view = $view;
		$this->feed = $feed;
	}

	public function render()
	{
		$info = new Flexical_Admin_Form_Table_Info();
		$info->request_type = ( $this->feed == null ) ? 'create_feed' : 'update_feed';
		$info->submit_button_text = ( $this->feed == null ) ? 'Create Feed' : 'Update Feed';
		$info->cancel_enabled = ( $this->feed == null ) ? false : true;

		if ( $this->feed == null )
		{
			$this->feed = new Flexical_Model_Feed();
		}

		else
		{
			$info->rows[ 'ID' ] = $this->feed->id;
			$info->hiddens[ 'feed_id' ] = $this->feed->id;
		}

		$info->rows[ 'Title' ] = "<input type=\"text\" name=\"feed_title\" size=\"50\" value=\"" . esc_html( $this->feed->title ) . "\" />";
		$info->rows[ 'URL' ] = "<input type=\"text\" name=\"feed_url\" size=\"50\" value=\"" . esc_html( $this->feed->url ) . "\" />";
		$info->rows[ 'Color' ] = "<input type=\"text\" name=\"feed_color\" value=\"" . esc_html( $this->feed->color ) . "\" />";
		$info->rows[ 'Timezone' ] = "<input type=\"text\" name=\"feed_timezone\" value=\"" . esc_html( $this->feed->timezone ) . "\" />";

		$this->view->render_form_table( $info );
	}
}

class Flexical_Admin_Feed_View extends Flexical_Admin_View
{
	public function get_selected_tab()
	{
		return $this->FEEDS_TAB;
	}

	public function get_messages( $response = null )
	{
		$messages = array();

		if ( $response instanceof Flexical_Admin_CUD_Response )
		{
			$content = null;

			switch ( $response->operation )
			{
				case Flexical_Admin_CUD_Response::$CREATE:
					$content = "Created feed " . $response->object->id . ': ' . $response->object->title;
					break;

				case Flexical_Admin_CUD_Response::$UPDATE:
					$content = "Updated feed " . $response->object->id . ': ' . $response->object->title;
					break;

				case Flexical_Admin_CUD_Response::$DELETE:
					$content = "Deleted feed " . $response->object->id . ': ' . $response->object->title;
					break;
				
				default:
					throw new Exception( "Unknown CUD response operation: " . $response->operation );
			}

			$messages[] = new Flexical_Admin_Info_Message( $content );
		}

		if ( $response instanceof Flexical_Admin_Show_Feeds_Response && $response->canceled )
		{
			$messages[] = new Flexical_Admin_Info_Message( "Action canceled." );
		}

		if ( $response instanceof Flexical_Admin_All_Feeds_Deleted_Response )
		{
			$messages[] = new Flexical_Admin_Info_Message( 'All feeds were deleted.' );
		}

		if ( $response instanceof Flexical_Admin_No_Such_Feed_Response )
		{
			$messages[] = new Flexical_Admin_Error_Message( 'No such feed ID: ' . $response->object->id );
		}

		if ( $response instanceof Flexical_Admin_Feed_Import_Succeeded_Response )
		{
			$messages[] = new Flexical_Admin_Info_Message( 'Feed import successful.' );
		}

		if ( $response instanceof Flexical_Admin_Feed_Import_Failed_Response )
		{
			$messages[] = new Flexical_Admin_Error_Message( 'Feed import failed: ' . $response->reason );
		}

		if ( $response instanceof Flexical_Admin_Delete_All_Feeds_Not_Confirmed_Response )
		{
			$messages[] = new Flexical_Admin_Error_Message( 'Delete all feeds request failed: The confirmation button was not checked.' );
		}

		if ( $response instanceof Flexical_Admin_Cant_Delete_Feed_In_Use_Response )
		{
			$text = '<p>Feed ' . $response->feed->id . ' "' . $response->feed->title . '" is used in one or more listings. ';
			$text .= "You must remove this feed from the listings or remove the listings before the feed can be removed.</p>\n";
			$text .= "<p>Listings that use this feed:</p>\n";
			$text .= $this->build_listings_in_use_table( $response->listings_using_feed );

			$messages[] = new Flexical_Admin_Error_Message( $text );
		}

		if ( $response instanceof Flexical_Admin_Cant_Delete_Feeds_In_Use_Response )
		{
			$messages[] = new Flexical_Admin_Error_Message( "You must remove all of the listings before you can remove all of the feeds." );
		}

		return $messages;
	}

	public function render_details( $response )
	{
		usort( $response->feeds, "Flexical_Model_Object::compare_by_title" );

		$this->render_feeds_table( $response->feeds );
		echo( "<p>&nbsp;</p>\n" );
		$this->render_feed_form();
		echo( "<p>&nbsp;</p>\n" );
		$this->render_import_feeds_form();
		echo( "<p>&nbsp;</p>\n" );

		if ( sizeof( $response->feeds ) > 0 )
		{
			$this->render_test_all_feeds_form();
			echo( "<p>&nbsp;</p>\n" );
			$this->render_delete_all_feeds_form();
			echo( "<p>&nbsp;</p>\n" );
		}
	}

	private function get_status( $status )
	{
		switch ( $status )
		{
			case 'good':
				return '<span style="color: green;">Good</span>';

			case 'bad':
				return '<span style="color: red;">Bad</span>';

			default:
				return '';
		}
	}

	private static function compare_feeds( $a, $b )
	{
		$al = strtolower( $a->title );
		$bl = strtolower( $b->title );

		return ( $al == $b ) ? 0 : ( $al < $bl ) ? -1 : +1;
	}

	private function render_feeds_table( $feeds = array() )
	{
		$table_headers = array( "ID", "Feed Title", "Color", "Timezone", "Status", "Actions" );

		echo( "<h3>Feeds</h3>\n" );
		echo( "<table class=\"widefat\">\n" );
		echo( "<thead><tr><th>" . implode( "</th><th>", $table_headers ) . "</th></tr></thead>\n" );


		foreach ( $feeds as $id => $feed )
		{
			echo( "<tr>" );
			echo( "<td>" . $feed->id . "</td>" );
			echo( "<td><a href=\"" . $feed->url . "\">" . esc_html( $feed->title ) . "</a></td>" );
			echo( "<td><span style=\"background-color: " . esc_html( $feed->color ) . ";\"> &nbsp; &nbsp; </span> &nbsp; " . $feed->color . "</td>" );
			echo( "<td>" . esc_html( $feed->timezone ) . "</td>" );
			echo( "<td>" . $this->get_status( $feed->status ) . "</td>" );
			echo( "<td>" .
				"<a href=\"" . $this->get_request_url( 'test_feed', array( 'feed_id' => $feed->id ) ) . "\">Test</a> | " .
				"<a href=\"" . $this->get_request_url( 'edit_feed', array( 'feed_id' => $feed->id ) ) . "\">Edit</a> | " .
				"<a href=\"" . $this->get_request_url( 'confirm_delete_feed', array( 'feed_id' => $feed->id ) ) . "\">Delete</a>" .
				"</td>" );
			echo( "</tr>\n" );
		}

		echo( "<tfoot><tr><th>" . implode( "</th><th>", $table_headers ) . "</th></tr></tfoot>\n" );
		echo( "</table>\n" );
	}

	private function render_feed_form( $feed = null )
	{
		echo( "<h3>Create a New Feed</h3>\n" );
		echo( "<p>Complete the form below to manually create a new reference to a Google Calendar feed.</p>\n" );
		$feed_form_renderer = new Flexical_Admin_Edit_Feed_Form_Renderer( $this, $feed );
		$feed_form_renderer->render();
	}

	private function render_import_feeds_form()
	{
		echo( "<h3>Import Feeds from Google Calendar</h3>\n" );
		echo( "<p>This action will import all of the feeds from a Google Calendar account. The Google Calendar username and password will not be saved in this application.</p>" );

		$info = new Flexical_Admin_Form_Table_Info();
		$info->request_type = 'import_feeds';
		$info->submit_button_text = 'Import Feeds';
		$info->cancel_enabled = false;

		$info->rows[ 'Google Username' ] = "<input type=\"text\" name=\"username\" />\n";
		$info->rows[ 'Google Password' ] = "<input type=\"password\" name=\"password\" />\n";
		$info->rows[ 'Overwrite or Append?' ] =
			"<input type=\"radio\" name=\"mode\" value=\"overwrite\" checked /> Overwrite current feeds with imported feed data &nbsp; " .
			"<input type=\"radio\" name=\"mode\" value=\"append\" /> Append imported feeds to list of current feeds";
		$info->rows[ 'Confirm'  ] = $this->get_confirmation_checkbox();

		$this->render_form_table( $info );
	}

	private function render_delete_all_feeds_form()
	{
		echo( "<h3>Delete All Feeds</h3>\n" );
		echo( "<p>This action will delete all of the feeds. <b>WARNING: This action cannot be undone.</b></p>" );

		$info = new Flexical_Admin_Form_Table_Info();
		$info->request_type = 'delete_all_feeds';
		$info->submit_button_text = 'Delete All Feeds';
		$info->cancel_enabled = false;
		$info->rows[ 'Confirm'  ] = $this->get_confirmation_checkbox();

		$this->render_form_table( $info );
	}

	private function render_test_all_feeds_form()
	{
		echo( "<h3>Test All Feeds</h3>\n" );
		echo( "<p>This action will run a test of all feeds to ensure that they can be accessed publicly. The results of the test will be shown in the Status column of the feeds table.</p>\n" );
		echo( "<p>NOTE: A feed may be marked as \"Bad\" for several reasons: the URL may be incorrect, the feed is not public, or the Google Calendar servers are overloaded and the feed isn't responding.</p>\n" );

		$info = new Flexical_Admin_Form_Table_Info();
		$info->request_type = 'test_all_feeds';
		$info->submit_button_text = 'Test All Feeds';
		$info->cancel_enabled = false;

		$this->render_form_table( $info );
	}
}

class Flexical_Admin_Confirm_Delete_Feed_View extends Flexical_Admin_View
{
	public function get_selected_tab()
	{
		return $this->FEEDS_TAB;
	}

	public function render_details( $response )
	{
		$feed = $response->feed;

		$info = new Flexical_Admin_Form_Table_Info();
		$info->request_type = 'delete_feed';
		$info->submit_button_text = 'Delete Feed';
		$info->cancel_enabled = true;

		$info->rows[ 'ID' ] = esc_html( $feed->id );
		$info->rows[ 'Title' ] = esc_html( $feed->title );
		$info->rows[ 'URL' ] = esc_html( $feed->url );
		$info->rows[ 'Color' ] = esc_html( $feed->color );
		$info->rows[ 'Timezone' ] = esc_html( $feed->timezone );
		$info->hiddens[ 'feed_id' ] = $feed->id;

		echo( "<h3>Are you sure you want to delete this feed?</h3>\n" );
		$this->render_form_table( $info );
	}
}

class Flexical_Admin_Edit_Feed_View extends Flexical_Admin_View
{
	public function get_selected_tab()
	{
		return $this->FEEDS_TAB;
	}

	public function render_details( $response )
	{
		echo( "<h3>Edit Feed</h3>\n" );
		$feed_form_renderer = new Flexical_Admin_Edit_Feed_Form_Renderer( $this, $response->feed );
		$feed_form_renderer->render();
	}
}

?>
